﻿using System.Collections.Generic;
using System.Configuration.Provider;
using System.Configuration;
using System.Threading.Tasks;
using System;

namespace JeanFish.Blog.Storage
{
    public abstract class BlogDataProvider : ProviderBase, IBlogDataProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
        }

        public abstract Task<IEnumerable<Post>> ListPostsAsync(Blog blog, Paging paging);

        public async virtual Task<Blog> LoadBlogAsync()
        {
            return await Task.FromResult(new Blog()
            {
                ID = ConfigurationManager.AppSettings["blogId"],
                Name = ConfigurationManager.AppSettings["blogName"],
                Onwer = ConfigurationManager.AppSettings["onwerId"],
            });
        }

        public async virtual Task<User> LoadBlogOnwerAsync(Blog blog)
        {
            return await Task.FromResult(new User()
            {
                ID = blog.Onwer,
                Name = ConfigurationManager.AppSettings["OnwerName"],
                Username = ConfigurationManager.AppSettings["username"],
                Password = ConfigurationManager.AppSettings["password"]
            });
        }

        public async Task<User> GetPostAuthorAsync(Post post)
        {
            return new User()
            {
                ID = (await LoadBlogAsync()).Onwer,
                Name = ConfigurationManager.AppSettings["OnwerName"]
            };
        }

        public abstract Task<SavedResult> SaveBlogAsync(Blog blog);


        public abstract Task<SavedResult> CreatePostAsync(Post post);

        public abstract Task<SavedResult> UpdatePostAsync(Post post);

        public abstract Task<Post> GetPostAsync(Blog blog, string postId);

    }
}
